package extractor;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import trouger.dmt.JoinTableRunner;
import trouger.dmt.JoinType;
import utils.Utils;

public class AttachBuyerCoec implements Tool{
	
	public static String getBuyerCoecPath(String date) {
		return Utils.rootDir + date + "/BuyerCoec/";
	}
	
	@Override 
	public Configuration getConf() {
		return null;
	}
	
	@Override
	public void setConf(Configuration conf) {
		
	}
	/*
	 * (non-Javadoc)
	 * UserFiltered Table
	 * Path: /group/tbsc-dev/xzwang/clickprediction/$date/BuyerCoec/
	 * ---------------------search---------------------
	 * 1. search[1~25]
	 * 26.UUID
	 * 27.pv时间
	 * 28.直达后类目
	 * 29.商品idlist
	 * 30. user[1~29]
	 * 59.clicklist[点击时间^A搜索的来源pid^A点击位置^A点击商品id(数字)^A是否为豆腐块商品^B....]
	 * 60.Buyer coec
	 * 61.Buyer ctr
	 * */	 
	@Override
	public int run(String[] args) throws Exception {
		String buyerFile = MergeBuyerFeature.getPvSearchClickWithBuyerPath(args[0]);

		String coecFile = "/group/tbsc-dev/xzwang/clickprediction/cyl/buyerFeature/coec";
		String output = getBuyerCoecPath(args[0]);
		System.out.println("start job ExtractUser...");
		System.out.println("buyerFile: " + buyerFile);
		System.out.println("coecFile: " + coecFile);
		System.out.println("output: " + output);
		if (Utils.exitSystem(args))
			return -1;
		JoinTableRunner r = new JoinTableRunner();
		r.addSourceTable(buyerFile, "UTF-8", "\t");
		r.addSourceTable(coecFile, "UTF-8", "\t");
		r.addKeyGroup("1.30, 2.1");	
		r.setJoinType(JoinType.LeftOuterJoin);
		StringBuilder columns = new StringBuilder();		
		for (int i = 1; i <= 59; ++i) {
			columns.append("1." + String.valueOf(i) + ", ");
		}
		columns.append("2.2,2.3");
		r.setResultFieldsEx(columns.toString());
		r.setResultTable(output);
		r.run("AttachBuyerCoecJob");
		return 0;		 	
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		Utils.checkArgs(args);
		
		// TODO Auto-generated method stub
		int res = ToolRunner.run(new AttachBuyerCoec(), args);
		System.exit(res);
	}
}